package com.vanzo.talkie2;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by tisen on 2018/1/17.
 */

public class ColorTextView extends TextView {
    public ColorTextView(Context context) {
        super(context);
    }

    public ColorTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ColorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ColorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private String title;
    private String value;
    private int titleColor = R.color.text_title_color;
    private int valueColor = R.color.text_value_color;
    private boolean underline;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        refresh();
    }

    public void setTitle(int title) {
        this.title = getResources().getString(title);
        refresh();
    }

    public int getTitleColor() {
        return titleColor;
    }

    public void setTitleColor(int titleColor) {
        this.titleColor = titleColor;
        refresh();
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
        refresh();
    }

    public void setValue(int value) {
        this.value = getResources().getString(value);
        refresh();
    }

    public int getValueColor() {
        return valueColor;
    }

    public void setValueColor(int valueColor) {
        this.valueColor = valueColor;
        refresh();
    }

    public boolean isUnderline() {
        return underline;
    }

    public void setUnderline(boolean underline) {
        this.underline = underline;
        refresh();
    }

    public void setText(String title, String value) {
        this.title = title;
        this.value = value;
        refresh();
    }

    public void setText(int title, int value) {
        this.title = getResources().getString(title);
        this.value = getResources().getString(value);
        refresh();
    }

    private void refresh() {
        SpannableString spannableString = new SpannableString(title + value);
        ForegroundColorSpan titleColorSpan = new ForegroundColorSpan(getResources().getColor(titleColor));
        ForegroundColorSpan valueColorSpan = new ForegroundColorSpan(getResources().getColor(valueColor));
        if (title != null)
            spannableString.setSpan(titleColorSpan, 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if (value != null)
            spannableString.setSpan(valueColorSpan, title.length(), spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if (underline) {
            spannableString.setSpan(new UnderlineSpan(), title.length(), spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {

        }
        setText(spannableString);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setUnderline(enabled);
    }
}
